package problem;

/**
 * @author RunningShrimp
 * @date 2021/6/10  15:44
 * @see <a href=""></a>
 */
public class RemoveDuplicate {
    public int removeDuplicates(int[] nums) {
        //记录不重复元素的索引
        int flag = 0;
        //记录重复元素
        int element = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != element) {
                int temp = nums[i];
                nums[i] = nums[flag];
                nums[flag] = temp;
                flag++;
            } else {
                element = nums[i];
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 2};
        int length = new RemoveDuplicate().removeDuplicates(nums);
        for (int i = 0; i < length; i++) {
            System.out.print(nums[i] + " ");
        }
    }
}
